package www.lagou.dao;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import www.lagou.entity.Account;
import www.lagou.entity.Transaction;
import www.lagou.utils.DateUtils;
import www.lagou.utils.DruidUtils;


import java.sql.Connection;
import java.sql.SQLException;

//需求1：实现卡号：1122334455向5544332211转装5000元的操作
/*    a) 使用连接池创建QueryRunner对象；
    b) 判断转出方是否有足够余额，如果不足，提示信息：”余额不足！”，并结束程序；
    c) 通过卡号 进行转账的操作；
    d) 转账结束后, 将转入、转出记录分别写入到Transaction表中。*/
//访问数据库的account表的数据
public class AcountDao {
    //根据boolean返回true和flase
    // accountCard01  转出
    // accountCard02  转入
    //double 转账的金额
    //public boolean transferMoney(String accountCard1,String accountCard2, Double money) throws SQLException


    public boolean transferMoney(String accountCard1,String accountCard2, double money) throws SQLException {
        //判断余额情况
        if (getBalanceFromAccountCard(accountCard1) < money) {
            System.out.println("余额不足！");
            return false;
        }
        //获取queryRunner,开启手动  去使用事务管理功能
        QueryRunner queryRunner = new QueryRunner();
        Connection connection = null;
        try {
            connection = DruidUtils.getConnection();
            connection.setAutoCommit(false);
            //编写SQL
            String sql = "update account set balance=balance + ? where card=?";
            //转出操作  money负数和正数
            Object[] paraml = {0 - money, accountCard1};
            queryRunner.update(connection, sql, paraml);
           // Transaction transaction1 = new Transaction(accountCard1, "转出", money, DateUtils.getDateFormart());
           Transaction transaction1=new Transaction(1,"1122334455","转出",money,DateUtils.getDateFormart());
            //转入操作  正数
            Object[] paraml2 = {money, accountCard2};
            queryRunner.update(connection, sql, paraml2);
            //Transaction transaction2 = new Transaction(accountCard2, "转入", money, DateUtils.getDateFormart());
            Transaction transaction2=new Transaction(2,"5544332211","转出",money,DateUtils.getDateFormart());
            //转账结束后，将转入，转出记录分别写入到Transaction表中
            TransactionDao transactionDao = new TransactionDao();
            if (transactionDao.addTransaction(transaction1) > 0) {
                System.out.println("转出记录已经写入到数据库");
            }
            if (transactionDao.addTransaction(transaction2) > 0) {
                System.out.println("转入记录已经写入到数据库");
            }
            DbUtils.commitAndCloseQuietly(connection);
        }catch (Exception e){
        DbUtils.rollbackAndCloseQuietly(connection);
        e.printStackTrace();
        return false;
        }finally {
        DbUtils.closeQuietly(connection);
        }
        return true;
    }
//根据卡号查询账户余额
    public  double getBalanceFromAccountCard(String accountCard) throws SQLException {
            QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
            String sql="select balance from account where card=?";
            return queryRunner.query(sql, new ScalarHandler<Double>(),accountCard);
    }
    //根据账户卡号查询账户所有信息
    public Account getAccountFromCard(String accountCard) throws SQLException {
        QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
        String sql="select * from account where card=?";
        return queryRunner.query(sql,new BeanHandler<Account>(Account.class),accountCard);
    }
}
